﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;

namespace XMK.Tadong.OfficeToSwf
{
    public class BkExecManager
    {
        //定时回调。 
        private static TimerCallback timerDelegate;
        private static Timer stateTimer;
        private static BkExecer m_Execer;
        /// <summary>
        /// 初始队列数
        /// </summary>
        public static int BkBufSize = 100;
        /// <summary>
        /// 间隔时间
        /// </summary>
        private static long Interval = 30000;
        /// <summary>
        /// 执行完一次后停顿的时间5秒
        /// </summary>
        private static long m_DueTime = 5000;
        /// <summary>
        /// 转换完pdf后停顿的时间
        /// </summary>
        public static int PDFDueTime = 30000;
        /// <summary>
        /// 转换完swf后停顿的时间
        /// </summary>
        public static int SWFDueTime = 30000;
        /// <summary>
        /// 每执行完一个队列项后停顿的时间
        /// </summary>
        public static int QueueItemDueTime = 10000;

        public static BkExecer Execer
        {
            get { return m_Execer; }
        }

        public BkExecManager(HttpContext curHttpContext)
        {
            //if (System.Configuration.ConfigurationManager.AppSettings["Interval"] != null)
            //    Interval = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Interval"]);
            //if (System.Configuration.ConfigurationManager.AppSettings["BkBufSize"] != null)
            //    BkBufSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["BkBufSize"]);
            //if (System.Configuration.ConfigurationManager.AppSettings["DueTime"] != null)
            //    m_DueTime = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["DueTime"]);
            //if (System.Configuration.ConfigurationManager.AppSettings["PDFDueTime"] != null)
            //    PDFDueTime = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PDFDueTime"]);
            //if (System.Configuration.ConfigurationManager.AppSettings["SWFDueTime"] != null)
            //    SWFDueTime = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SWFDueTime"]);
            m_Execer = new BkExecer(curHttpContext);
            //初始化回调 
            timerDelegate = new TimerCallback(m_Execer.DoBkExec);
            //初始化定时器 
            //四个参数分别表示：要执行的方法；包括回调方法中要使用的信息的对象；在调用执行的方法之前的延迟时间（毫秒）；调用回调方法的时间间隔（毫秒）
            stateTimer = new Timer(timerDelegate, null, m_DueTime, Interval);
        }

        /// <summary>
        /// 启动执行
        /// </summary>
        /// <returns></returns>
        public static bool Start()
        {
            try
            {
                if (stateTimer != null)
                {
                    stateTimer.Dispose();
                }
                stateTimer = new Timer(timerDelegate, null, m_DueTime, Interval);
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }

        }
        /// <summary>
        /// 设置时间
        /// </summary>
        /// <param name="duetime"></param>
        /// <param name="periodtime"></param>
        /// <returns></returns>
        public bool change(long duetime, long periodtime)
        {
            if (stateTimer == null)
            {
                return false;
            }
            Interval = periodtime;
            stateTimer.Change(duetime, periodtime);
            return true;
        }

        /// <summary>
        /// 停止执行
        /// </summary>
        /// <returns></returns>
        public static bool stop()
        {
            try
            {
                if (stateTimer != null)
                {
                    stateTimer.Dispose();
                    stateTimer = null;
                }
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }
        }


    }
}
